﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Runtime.Serialization;

namespace Data
{
    [DataContract]
    public class Playlist : IPlayable
    {
        #region Объектная модель
        static protected readonly Dictionary<Guid, Playlist> _allPlaylists = new Dictionary<Guid, Playlist>();

        static public Playlist GetById(Guid id)
        {
            if (_allPlaylists.ContainsKey(id))
            {
                return _allPlaylists[id];
            }
            else
            {
                return null;
            }
        }
        #endregion

        static private String _tableName = "[playlists]";
        static public String TableName 
        {
            get { return _tableName; }
        }

        public Playlist(SqlDataReader reader)
        {
            _id = new Guid((String)reader["id"]);
            _id_user = (String)reader["id_user"];
            _name = (String)reader["name"];
        }

        static public void AddToAllPlaylists(Playlist list)
        {
            if (null != list)
            {
                if (!_allPlaylists.Keys.Contains(list.ID))
                {
                    _allPlaylists.Add(list.ID, list);
                }
            }
        }

        #region Serializeable
        [DataMember]
        protected Guid _id;
        public Guid ID
        {
            get { return _id; }
        }

        [DataMember]
        private String _id_user;
        public User User
        {
            get
            {
                return User.GetByName(_id_user);
            }
        }

        [DataMember]
        String _name;
        public String Name
        {
            get { return _name; }
        }
        #endregion

        public List<PlaylistConnector> Connectors
        {
            get
            {
                return PlaylistConnector.GetByPlaylist(this);
            }
        }

        public List<Song> Songs
        {
            get
            {
                List<Song> tmp_list = new List<Song>();

                foreach (var connector in this.Connectors)
                {
                    tmp_list.Add(connector.Song);
                }

                return tmp_list;
            }
        }

        #region Члены IPlayable

        String IPlayable.Text
        {
            get 
            {
                return "Playlist \"" + this.Name + "\" by " + this.User.Name;
            }
        }

        IPlayable IPlayable.Next()
        {
            throw new NotImplementedException();
        }

        IPlayable IPlayable.Previous()
        {
            throw new NotImplementedException();
        }

        void IPlayable.Stop()
        {
            throw new NotImplementedException();
        }

        void IPlayable.Pause()
        {
            throw new NotImplementedException();
        }


        public override string ToString()
        {
            return ((IPlayable)this).Text;
        }
        #endregion
    }
}
